package com.r2coding.article.controller;

import com.r2coding.article.annotation.SysLog;
import com.r2coding.article.vo.req.UpdateCategoryReqVo;
import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.article.entity.CategoryEntity;
import com.r2coding.article.service.CategoryService;
import com.r2coding.common.util.Result;

import java.util.Date;

/**
 *  分类信息控制器
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-13 19:34:16
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 信息
     */
    @GetMapping("/info/{categoryId}")
    public Result info(@PathVariable("categoryId") Integer categoryId){
		CategoryEntity category = categoryService.getById(categoryId);
        return Result.ok(category);
    }

    /**
     * 保存
     */
    //@CacheEvict(value = {"base"}, key = " 'selectBase' ") // 删除缓存
    @SysLog(module = Module.ARTICLE, operationType = OperationType.INSERT, description = "添加分类")
    @PreAuthorize("hasAuthority('type:add')")
    @PostMapping("/save")
    public Result save(@RequestBody CategoryEntity category){
        category.setUpdateTime(new Date());
        category.setDeleted(0);
        category.setCreateTime(new Date());
		categoryService.save(category);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAuthority('type:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody UpdateCategoryReqVo reqVo){
		categoryService.updateCategory(reqVo);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAuthority('type:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
		categoryService.deleteCategory(id);
        return Result.ok();
    }
}
